<template>
    <div class="app-container">
        <el-form
            :model="queryParams"
            ref="queryForm"
            size="small"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
        >
            <el-form-item label="用人部门" prop="demandId">
                <el-input
                    v-model="queryParams.deptName"
                    placeholder="请填写用人部门"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item label="岗位" prop="demandId">
                <el-input
                    v-model="queryParams.postName"
                    placeholder="请输入岗位"
                    clearable
                    @keyup.enter.native="handleQuery"
                />
            </el-form-item>
            <el-form-item>
                <el-button
                    type="primary"
                    icon="el-icon-search"
                    size="mini"
                    @click="handleQuery"
                    >查询</el-button
                >
                <el-button
                    icon="el-icon-refresh"
                    size="mini"
                    @click="resetQuery"
                    >重置</el-button
                >
            </el-form-item>
        </el-form>

        <TableCard>
            <el-row :gutter="10" class="mb8" v-if="!readonly">
                <el-col :span="1.5">
                    <el-button
                        type="primary"
                        plain
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleAdd"
                        v-hasPermi="['hrm:recruitDemand:add']"
                    >
                        新增
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="success"
                        plain
                        icon="el-icon-edit"
                        size="mini"
                        :disabled="single"
                        @click="handleUpdate"
                        v-hasPermi="['hrm:recruitDemand:edit']"
                    >
                        修改
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="danger"
                        plain
                        icon="el-icon-delete"
                        size="mini"
                        :disabled="multiple"
                        @click="handleDelete"
                        v-hasPermi="['hrm:recruitDemand:remove']"
                    >
                        删除
                    </el-button>
                </el-col>
                <el-col :span="1.5">
                    <el-button
                        type="warning"
                        plain
                        icon="el-icon-download"
                        size="mini"
                        @click="handleExport"
                        v-hasPermi="['hrm:recruitDemand:export']"
                    >
                        导出
                    </el-button>
                </el-col>
                <right-toolbar
                    :showSearch.sync="showSearch"
                    @queryTable="getList"
                ></right-toolbar>
            </el-row>

            <el-table
                :border="Global.tableShowBorder"
                v-loading="loading"
                :data="recruitDemandItemList"
                @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center" />
                <el-table-column
                    label="需求部门"
                    align="center"
                    prop="deptName"
                />
                <el-table-column label="岗位" align="center" prop="postName" />
                <el-table-column
                    label="需要数量"
                    align="center"
                    prop="needCount"
                />
                <el-table-column
                    label="薪资范围"
                    align="center"
                    prop="salaryMin"
                >
                    <template slot-scope="scope">
                        {{ scope.row.salaryMin }} - {{ scope.row.salaryMax }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="岗位需求"
                    align="center"
                    prop="postDemand"
                >
                    <template slot-scope="scope">
                        <el-tooltip placement="top">
                            <div slot="content">
                                <div v-html="scope.row.postDemand"></div>
                            </div>
                            <el-button size="mini" type="text"
                                >岗位需求</el-button
                            >
                        </el-tooltip>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    class-name="small-padding fixed-width"
                    v-if="!readonly"
                >
                    <template slot-scope="scope">
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-edit"
                            @click="handleUpdate(scope.row)"
                            v-hasPermi="['hrm:recruitDemand:edit']"
                        >
                            修改
                        </el-button>
                        <el-button
                            size="mini"
                            type="text"
                            icon="el-icon-delete"
                            @click="handleDelete(scope.row)"
                            v-hasPermi="['hrm:recruitDemand:remove']"
                        >
                            删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination
                v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList"
            />
        </TableCard>

        <!-- 添加或修改招聘需求明细对话框 -->
        <el-dialog
            :title="title"
            :visible.sync="open"
            width="1000px"
            append-to-body
        >
            <el-form
                ref="form"
                :model="form"
                :rules="rules"
                label-width="120px"
            >
                <el-row :gutter="10" class="mb8">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12">
                        <el-form-item label="需求部门" prop="deptId">
                            <treeselect
                                v-model="form.deptId"
                                :options="deptOptions"
                                :normalizer="normalizer"
                                placeholder="请选择需求部门"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10" class="mb8">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12">
                        <el-form-item label="岗位名称" prop="needCount">
                            <el-select
                                v-model="form.postId"
                                placeholder="请选择岗位"
                            >
                                <el-option
                                    v-for="item in postOptions"
                                    :key="item.postId"
                                    :label="item.postName"
                                    :value="item.postId"
                                    :disabled="item.status == 1"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12">
                        <el-form-item label="需要数量" prop="needCount">
                            <el-input
                                v-model="form.needCount"
                                type="number"
                                placeholder="请输入需要数量"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10" class="mb8">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12">
                        <el-form-item label="最低薪资" prop="deptId">
                            <el-input
                                v-model="form.salaryMin"
                                type="number"
                                placeholder="请输入最低薪资"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12">
                        <el-form-item label="最高薪资" prop="deptId">
                            <el-input
                                v-model="form.salaryMax"
                                type="number"
                                placeholder="请输入最高薪资"
                            />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="岗位需求">
                    <editor v-model="form.postDemand" :min-height="192" />
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listRecruitDemandItem,
    getRecruitDemandItem,
    delRecruitDemandItem,
    addRecruitDemandItem,
    updateRecruitDemandItem,
} from "@/api/hrm/recruitDemandItem";
import { listDept } from "@/api/system/dept";
import { listPostAll } from "@/api/system/post";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
    name: "RecruitDemandItem",
    components: { Treeselect },
    props: {
        readonly: {
            type: Boolean,
            default: false,
        },
        ifTab: {
            type: Boolean,
            default: false,
        },
        demandId: {
            type: [Number, String],
            default: null,
        },
    },
    data() {
        return {
            // 职位选项
            postOptions: [],
            // 部门树选项
            deptOptions: [],
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示查询条件
            showSearch: true,
            // 总条数
            total: 0,
            // 招聘需求明细表格数据
            recruitDemandItemList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 20,
                demandId: null,
                deptId: null,
                postId: null,
                deptName: null,
                postName: null,
                needCount: null,
                postDemand: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {},
        };
    },
    created() {
        //获取组织结构树
        this.getDeptTreeData();
        //获取岗位数据
        this.getPostData();
        console.log(typeof this.demandId);
        console.log(
            "是否在页签中展示：" + this.ifTab + "。需求id：" + this.demandId
        );
        // 如果是从客户管理页面传过来的
        if (this.ifTab && this.demandId) {
            //设置查询条件
            this.queryParams.demandId = this.demandId;
        }
        this.getList();
    },
    methods: {
        getDeptTreeData() {
            listDept().then((response) => {
                this.deptOptions = this.handleTree(response.data, "deptId");
            });
        },
        getPostData() {
            listPostAll().then((res) => {
                this.postOptions = res.data;
            });
        },
        /** 转换部门数据结构 */
        normalizer(node) {
            if (node.children && !node.children.length) {
                delete node.children;
            }
            return {
                id: node.deptId,
                label: node.deptName,
                children: node.children,
            };
        },
        /** 查询招聘需求明细列表 */
        getList() {
            this.loading = true;
            listRecruitDemandItem(this.queryParams).then((response) => {
                this.recruitDemandItemList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                demandId: null,
                deptId: null,
                postId: null,
                needCount: null,
                postDemand: null,
            };
            this.resetForm("form");
        },
        /** 查询按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.queryParams.deptName = "";
            this.queryParams.postName = "";
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.id);
            this.single = selection.length !== 1;
            this.multiple = !selection.length;
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            // 如果是从客户管理页面传过来的
            if (this.ifTab && this.demandId) {
                this.form.demandId = this.demandId;
            }
            this.open = true;
            this.title = "添加招聘需求明细";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids;
            getRecruitDemandItem(id).then((response) => {
                this.form = response.data;
                this.open = true;
                this.title = "修改招聘需求明细";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.form.id != null) {
                        updateRecruitDemandItem(this.form).then((response) => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addRecruitDemandItem(this.form).then((response) => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal
                .confirm(
                    '是否确认删除招聘需求明细编号为"' + ids + '"的数据项？'
                )
                .then(function () {
                    return delRecruitDemandItem(ids);
                })
                .then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                })
                .catch(() => {});
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download(
                "hrm/recruitDemandItem/export",
                {
                    ...this.queryParams,
                },
                `recruitDemandItem_${new Date().getTime()}.xlsx`
            );
        },
    },
};
</script>
